document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('register-form');
    const usernameInput = document.getElementById('reg-username');
    const passwordInput = document.getElementById('reg-password');
    const confirmPasswordInput = document.getElementById('reg-confirm-password');
    const emailInput = document.getElementById('reg-email');
    const agreeInput = document.getElementById('reg-agree');
    const usernameError = document.getElementById('reg-username-error');
    const passwordError = document.getElementById('reg-password-error');
    const confirmPasswordError = document.getElementById('reg-confirm-password-error');
    const emailError = document.getElementById('reg-email-error');
    const genderError = document.getElementById('gender-error');
    const agreeError = document.getElementById('reg-agree-error');

    form.addEventListener('submit', function(e) {
        e.preventDefault();
        let isValid = true;

        // 验证用户名（只能包含字母）
        if (!/^[a-zA-Z]+$/.test(usernameInput.value.trim())) {
            usernameError.textContent = '用户名只能包含字母';
            usernameInput.classList.add('error');
            isValid = false;
        } else {
            usernameError.textContent = '';
            usernameInput.classList.remove('error');
        }

        // 验证密码（必须包含字母、数字、下划线，且长度≥6）
        if (!/(?=.*[a-zA-Z])(?=.*\d)(?=.*_).{6,}/.test(passwordInput.value)) {
            passwordError.textContent = '密码必须包含字母、数字和下划线，且长度至少为6位';
            passwordInput.classList.add('error');
            isValid = false;
        } else {
            passwordError.textContent = '';
            passwordInput.classList.remove('error');
        }

        // 验证确认密码
        if (confirmPasswordInput.value !== passwordInput.value) {
            confirmPasswordError.textContent = '两次输入的密码不一致';
            confirmPasswordInput.classList.add('error');
            isValid = false;
        } else {
            confirmPasswordError.textContent = '';
            confirmPasswordInput.classList.remove('error');
        }

        // 验证邮箱
        if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailInput.value.trim())) {
            emailError.textContent = '请输入有效的电子邮箱地址';
            emailInput.classList.add('error');
            isValid = false;
        } else {
            emailError.textContent = '';
            emailInput.classList.remove('error');
        }

        // 验证性别是否选择
        const genderSelected = document.querySelector('input[name="gender"]:checked');
        if (!genderSelected) {
            genderError.textContent = '请选择性别';
            isValid = false;
        } else {
            genderError.textContent = '';
        }

        // 如果验证通过，提交表单
        if (isValid) {
            alert('注册成功！请登录');
            window.location.href = 'login.html'; // 注册成功后跳转到登录页
        } else {
            // 聚焦到第一个错误字段
            const firstError = document.querySelector('.error');
            if (firstError) firstError.focus();
        }
    });
});
